package com.haima.ziliao.controller;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.annotation.RepeatSubmit;
import com.haima.common.utils.DateUtils;
import com.haima.common.utils.SecurityUtils;
import com.haima.ziliao.domain.*;
import com.haima.ziliao.service.IPDayinconfigService;
import com.haima.ziliao.service.IPUservipService;
import com.haima.ziliao.service.IPZiliaoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.ziliao.service.IPDayinCarService;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 打印车Controller
 * 
 * @author hima
 * @date 2024-10-21
 */
@RestController
@RequestMapping("/ziliao/dayincar")
public class PDayinCarController extends BaseController
{
    @Autowired
    private IPDayinCarService pDayinCarService;
    @Autowired
    private IPZiliaoService ziliaoService;
    @Autowired
    private IPDayinconfigService dayinconfigService;
    @Autowired
    private IPUservipService uservipService;

    /**
     * 查询打印车列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:list')")
    @GetMapping("/list")
    public TableDataInfo list(PDayinCar pDayinCar)
    {
        startPage();
        List<PDayinCar> list = pDayinCarService.selectPDayinCarList(pDayinCar);
        return getDataTable(list);
    }

    @GetMapping("/getlist")
    public AjaxResult getlist(PDayinCar pDayinCar)
    {
        pDayinCar.setUid(SecurityUtils.getUserId());
        List<PDayinCar> list = pDayinCarService.selectPDayinCarList(pDayinCar);
        return AjaxResult.success(list);
    }

    @GetMapping("/getDayinCarNum")
    public AjaxResult getDayinCarNum()
    {
        Long num = pDayinCarService.count(new QueryWrapper<PDayinCar>().eq("uid",SecurityUtils.getUserId()).eq("status",1));
        return AjaxResult.success(num);
    }

    @GetMapping("/deleteCar")
    @RepeatSubmit
    public AjaxResult deleteCar(PDayinCar pDayinCar)
    {
        pDayinCar = pDayinCarService.selectPDayinCarById(pDayinCar.getId());

        pDayinCar.setStatus(2);

        pDayinCarService.updatePDayinCar(pDayinCar);

        return AjaxResult.success();
    }

    @GetMapping("/changeGeShiById")
    @RepeatSubmit
    public AjaxResult changeGeShiById(PDayinCar pDayinCar)
    {
        PUservip uvip = uservipService.getOne(new QueryWrapper<PUservip>().eq("uid",SecurityUtils.getUserId()).eq("status",1));
        PDayinCar car = pDayinCarService.selectPDayinCarById(pDayinCar.getId());
        PDayinconfig config = dayinconfigService.selectPDayinconfigById(1L);
        car.setFs(pDayinCar.getFs());
        car.setGeshi(pDayinCar.getGeshi());

        JSONObject json = JSONObject.parseObject(car.getGeshi());
        String danshuang = json.getString("danshuang");
        String zhuangding = json.getString("zhuangding");

        BigDecimal jine = new BigDecimal("0");

        if("单面打印".equals(danshuang)){

            if(uvip!=null){
                BigDecimal dm = config.getDanmianvip();
                jine = dm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
            }else{
                BigDecimal dm = config.getDanmian();
                jine = dm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
            }
        } else if("双面打印".equals(danshuang)){
            BigDecimal sm = config.getShuangmian();
            jine = sm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
        }

        if("胶装".equals(zhuangding)){
            if(uvip!=null){
                jine = jine.add(config.getJiaozhuangvip());
            }else{
                jine = jine.add(config.getJiaozhuang());
            }
        } else if("订书钉".equals(zhuangding)){
            if(uvip!=null){
                jine = jine.add(config.getDingshudingvip());
            }else{
                jine = jine.add(config.getDingshuding());
            }
        } else if("骑马钉".equals(zhuangding)){
            if(uvip!=null){
                jine = jine.add(config.getQimadingvip());
            }else{
                jine = jine.add(config.getQimading());
            }
        }
        car.setJine(jine);



        pDayinCarService.updatePDayinCar(car);
        return AjaxResult.success();
    }
    @GetMapping("/changeGeShiAll")
    @RepeatSubmit
    public AjaxResult changeGeShiAll(PDayinCar pDayinCar)
    {

        PUservip uvip = uservipService.getOne(new QueryWrapper<PUservip>().eq("uid",SecurityUtils.getUserId()).eq("status",1));

        PDayinconfig config = dayinconfigService.selectPDayinconfigById(1L);

        PDayinCar cars = new PDayinCar();
        cars.setUid(SecurityUtils.getUserId());
        cars.setStatus(1);
        List<PDayinCar> listcar = pDayinCarService.selectPDayinCarList(cars);

        for(int i=0;i<listcar.size();i++){
            PDayinCar car = listcar.get(i);

            car.setFs(pDayinCar.getFs());
            car.setGeshi(pDayinCar.getGeshi());

            JSONObject json = JSONObject.parseObject(car.getGeshi());
            String danshuang = json.getString("danshuang");
            String zhuangding = json.getString("zhuangding");

            BigDecimal jine = new BigDecimal("0");

            if("单面打印".equals(danshuang)){

                if(uvip!=null){
                    BigDecimal dm = config.getDanmianvip();
                    jine = dm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
                }else{
                    BigDecimal dm = config.getDanmian();
                    jine = dm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
                }
            } else if("双面打印".equals(danshuang)){
                BigDecimal sm = config.getShuangmian();
                jine = sm.multiply(new BigDecimal(car.getYeshu())).multiply(new BigDecimal(car.getFs()));
            }

            if("胶装".equals(zhuangding)){
                if(uvip!=null){
                    jine = jine.add(config.getJiaozhuangvip());
                }else{
                    jine = jine.add(config.getJiaozhuang());
                }
            } else if("订书钉".equals(zhuangding)){
                if(uvip!=null){
                    jine = jine.add(config.getDingshudingvip());
                }else{
                    jine = jine.add(config.getDingshuding());
                }
            } else if("骑马钉".equals(zhuangding)){
                if(uvip!=null){
                    jine = jine.add(config.getQimadingvip());
                }else{
                    jine = jine.add(config.getQimading());
                }
            }
            car.setJine(jine);
            pDayinCarService.updatePDayinCar(car);
        }
        return AjaxResult.success();
    }

    /**
     * 导出打印车列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:export')")
    @Log(title = "打印车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PDayinCar pDayinCar)
    {
        List<PDayinCar> list = pDayinCarService.selectPDayinCarList(pDayinCar);
        ExcelUtil<PDayinCar> util = new ExcelUtil<PDayinCar>(PDayinCar.class);
        util.exportExcel(response, list, "打印车数据");
    }

    @GetMapping("/addFils")
    @RepeatSubmit
    public AjaxResult addFils(Long fid)
    {
        PDayinCar pc = pDayinCarService.getOne(new QueryWrapper<PDayinCar>().eq("fileId",fid).eq("uid", SecurityUtils.getUserId()).eq("status",1));

        PDayinconfig config = dayinconfigService.selectPDayinconfigById(1L);

        if(pc == null){
            PZiliao zl = ziliaoService.selectPZiliaoById(fid);
            pc = new PDayinCar();
            pc.setFs(1);
            pc.setAddTime(DateUtils.getNowDate());
            pc.setUid(SecurityUtils.getUserId());
            pc.setFileId(fid);
            pc.setFilePath(zl.getPath());
            pc.setName(zl.getTitle());
            pc.setYeshu(zl.getPageNum());

            Integer num = pc.getYeshu();

            BigDecimal jine = new BigDecimal("0");
            //获取会员信息

            PUservip uvip = uservipService.getOne(new QueryWrapper<PUservip>().eq("uid",SecurityUtils.getUserId()).eq("status",1));

            if(uvip!=null){
                BigDecimal dm = config.getDanmianvip();
                jine = dm.multiply(new BigDecimal(num)).multiply(new BigDecimal(pc.getFs()));
            }else{
                BigDecimal dm = config.getDanmian();
                jine = dm.multiply(new BigDecimal(num)).multiply(new BigDecimal(pc.getFs()));
            }
            pc.setJine(jine);
            pDayinCarService.insertPDayinCar(pc);
        }
        return AjaxResult.success("添加成功");
    }

    /**
     * 获取打印车详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pDayinCarService.selectPDayinCarById(id));
    }

    /**
     * 新增打印车
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:add')")
    @Log(title = "打印车", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PDayinCar pDayinCar)
    {
        return toAjax(pDayinCarService.insertPDayinCar(pDayinCar));
    }

    /**
     * 修改打印车
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:edit')")
    @Log(title = "打印车", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PDayinCar pDayinCar)
    {
        return toAjax(pDayinCarService.updatePDayinCar(pDayinCar));
    }

    /**
     * 删除打印车
     */
    @PreAuthorize("@ss.hasPermi('ziliao:dayincar:remove')")
    @Log(title = "打印车", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pDayinCarService.deletePDayinCarByIds(ids));
    }
}
